#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->splitterMain->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_model  =  new QFileSystemModel(this);
    m_model->setRootPath(QDir::currentPath());
    ui->treeView ->setModel(m_model);
    ui->listView->setModel(m_model);
    ui->tableView->setModel(m_model);

    connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->listView, SLOT(setRootIndex(QModelIndex)));
    connect(ui->treeView, SIGNAL(clicked(QModelIndex)), ui->tableView, SLOT(setRootIndex(QModelIndex)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
    ui->chkIsDir->setChecked(m_model->isDir(index));
    ui->labPath->setText(m_model->filePath(index));
    ui->labFileName->setText(m_model->fileName(index));
    ui->labType->setText(m_model->type(index));
    int sz = m_model->size(index) / 1024;
    if(sz < 1024)
        ui->labFileSize->setText(QString("%1 KB").arg(sz));
    else
        ui->labFileSize->setText(QString::asprintf("%.1fMB", sz / 1024.0));
}


void MainWindow::on_actSetRoot_triggered()
{
    QString dir = QFileDialog::getExistingDirectory(this, "选择目录", QDir::currentPath());
    if(!dir.isEmpty())
    {
        m_model->setRootPath(dir);
        ui->treeView->setRootIndex(m_model->index(dir));
    }
}

void MainWindow::on_radioShowAll_clicked()
{
    ui->groupBoxFilter->setEnabled(true);
    m_model->setFilter(QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
}

void MainWindow::on_radioShowOnlyDir_clicked()
{
    ui->groupBoxFilter->setEnabled(false);
    m_model->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
}


void MainWindow::on_chkBoxEnableFilter_clicked(bool checked)
{
    m_model->setNameFilterDisables(!checked);
    ui->comboFilters->setEnabled(checked);
    ui->btnApplyFilters->setEnabled(checked);
}

void MainWindow::on_btnApplyFilters_clicked()
{
    QString fits = ui->comboFilters->currentText().trimmed();
    QStringList filter = fits.split(";", Qt::SkipEmptyParts);
    m_model->setNameFilters(filter);
}

